<html>
<head>
    <meta charset="UTF-8">
    <title>角色管理-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../static/css/font.css">
    <link rel="stylesheet" href="../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<style>
    .layui-table-cell .layui-form-checkbox[lay-skin=primary] {
        top: 5px;
    }
</style>
<div class="weadmin-nav">
			<span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">管理员管理</a>
        <a>
          <cite>角色管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="weadmin-body">
    <div class="weadmin-block">
        <button class="layui-btn layui-btn-danger layui-btn-sm" onclick="delAll()"><i class="layui-icon"></i>批量删除
        </button>
        <button class="layui-btn layui-btn-sm" onclick="WeAdminShow('新增','/admin/auth_rule/add')"><i class="layui-icon"></i>添加
        </button>
        <button class="layui-btn layui-btn-warm layui-btn-sm" onclick="openAll();"><i class="layui-icon"></i>展开或折叠全部
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="reload()"><i
                class="layui-icon layui-icon-refresh"></i>刷新
        </button>
    </div>
    <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
</div>
<script>
    var editObj = null, ptable = null, treeGrid = null, tableId = 'treeTable', layer = null, $ = null;
    layui.config({
        base: '/lib/layui/lay/modules/'
    }).extend({
        treeGrid: 'treeGrid',
        admin: '{/}../../static/js/admin',
    }).use(['jquery', 'treeGrid', 'admin', 'layer'], function () {
        var admin = layui.admin;
        $ = layui.jquery;
        var table = layui.table;
        treeGrid = layui.treeGrid;//很重要
        layer = layui.layer;
        ptable = treeGrid.render({
            id: tableId
            , elem: '#' + tableId
            , url: '/api/auth_rule/index'
            , cellMinWidth: 100
            , idField: 'id'//必須字段
            , treeId: 'id'//树形id字段名称
            , treeUpId: 'pid'//树形父id字段名称
            , treeShowName: 'title'//以树形式显示的字段
            , heightRemove: [".dHead", 10]//不计算的高度,表格设定的是固定高度，此项不生效
            , height: '100%'
            , isFilter: false
            , iconOpen: true//是否显示图标【默认显示】
            , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
            , loading: true
            , method: 'post'
            , isPage: false
            , cols: [[
                // ,{type:'radio'}
                {type: 'checkbox', sort: true}
                , {type: 'numbers'}
                , {field: 'title', width: 300, title: '节点名称'}
                , {field: 'name', width: 300, title: '节点地址'}
                , {field: 'lay_icon', width: 200, title: '图标'}
                , {field: 'sorts', width: 100, title: '排序'}
                , {
                    width: 100, title: '是否显示', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        if (d.status == 1) {
                            return '<a lay-event="status">' + '<input type="checkbox" lay-event="status-close" name="status" lay-skin="switch" lay-text="显示|隐藏" checked>' + '</a>';
                        } else {
                            return '<a lay-event="status">' + '<input type="checkbox" lay-event="status-open" name="status" lay-skin="switch" lay-text="显示|隐藏" >' + '</a>';
                        }
                    }
                }
                , {
                    width: 100, title: '是否菜单', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        if (d.ismenu == 1) {
                            return '<a lay-event="menu">' + '<input type="checkbox" lay-event="menu-close" name="ismenue" lay-skin="switch" lay-text="是|否" checked>' + '</a>';
                        } else {
                            return '<a lay-event="menu">' + '<input type="checkbox" lay-event="menu-open" name="ismenue" lay-skin="switch" lay-text="是|否">' + '</a>';
                        }
                    }
                }
                , {
                    width: 250, title: '操作', align: 'center'/*toolbar: '#barDemo'*/
                    , templet: function (d) {
                        var disable = '';
                        if (d.level > 2) {
                            var addBtn = '<a class="layui-btn layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-add-1"></i>添加子权限</a>';
                        } else {
                            var addBtn = '<a class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加子权限</a>';
                        }
                        var editBtn = '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                        var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete">删除</a>';
                        return addBtn + editBtn + delBtn;
                    }
                }
            ]]
            , parseData: function (res) {//数据加载后回调
                return res;
            }
            // ,onCheck:function (obj,checked,isAll) {//复选事件
            //     console.log(obj,checked,isAll,"复选");
            //     msg("复选,按F12，在控制台查看详细参数！");
            // }
            // ,onRadio:function (obj) {//单选事件
            //     console.log(obj,"单选");
            //     msg("单选,按F12，在控制台查看详细参数！");
            // }
        });
        //监听单元格编辑

        treeGrid.on('tool(' + tableId + ')', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {//删除行
                del(obj);
            } else if (obj.event === "add") {//添加行
                WeAdminShow('添加', '/admin/auth_rule/add?id=' + data.id);
            } else if (obj.event === "edit") {//隐藏
                WeAdminShow('编辑', '/admin/auth_rule/edit?id=' + data.id);
            } else if (obj.event === "status") {//隐藏
               ajax_update(data.id,{status:1});
            } else if (obj.event === "menu") {//显示
               ajax_update(data.id,{ismenu:1});
            }
        });

    });

    function ajax_update(id,json) {
        $.ajax({
            url: '/admin/auth_rule/ajax/id/' + id,
            method: 'post',
            data: json,
            dataType: 'JSON',
            success: function (res) {
                if (res.code == 0) {
                    layer.msg('修改成功', {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 5});
                    reload();
                }
            },
            error: function (data) {
                layer.msg('服务器繁忙', {icon: 5});
                reload();
            }
        })
    }

    function del(obj) {
        layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除，此操作不能撤销！", {icon: 3, title: '提示'},
            function (index) {//确定回调
                let data = {};
                data.id = obj.data.id;
                $.ajax({
                    url: '/admin/auth_rule/delete',
                    method: 'post',
                    data: data,
                    dataType: 'JSON',
                    success: function (res) {
                        //基本演示
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                layer.close(index);
            }, function (index) {//取消回调
                layer.close(index);
            }
        );
    }

    //删除所选的全部
    function delAll() {
        var checkStatus = treeGrid.checkStatus(tableId)
            , data = checkStatus.data;
        var ids = [];
        for (var i = 0; i < data.length; i++) {
            ids.push(data[i].id);
        }
        layer.confirm("你确定删除" + data.length + "条数据吗？此操作不能撤销！", {icon: 3, title: '提示'},
            function (index) {//确定回调
                let sdata = {};
                sdata.id = ids;
                $.ajax({
                    url: '/admin/auth_rule/delete',
                    method: 'post',
                    data: sdata,
                    dataType: 'JSON',
                    success: function (res) {
                        //基本演示
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            checkStatus.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                layer.close(index);
            }, function (index) {//取消回调
                layer.close(index);
            }
        );

    }
    function openAll() {
        var treedata=treeGrid.getDataTreeList(tableId);
        treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
    }
    function reload() {
        treeGrid.reload(tableId, {
            page: {
                curr: 1
            }
        });
    }

</script>
</body>
</html>